package com.yyfax.ph.home.datainit;

import android.os.Parcel;
import android.os.Parcelable;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

import java.io.Serializable;
import java.util.List;

public class OptionsModel extends Model {
	private static final String TAG = OptionsModel.class.getSimpleName();

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj.toString());
		OptionResult result = cast(obj);
		EventAgent.post(result);
	}

	@Override
	public Observable getObservable(Object obj, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		return service.getOptions(token, new Option((String) obj));
	}

	private interface Service {
		@POST("sys/option/get")
		Observable<OptionResult> getOptions(@Header("token") String token, @Body Option option);
	}

	private static class Option {
		String types;

		Option(String types) {
			this.types = types;
		}
	}

	static class OptionResult extends BaseResultEntity<OptionsBean> {
	}

	public static class OptionBean implements Serializable, Parcelable {

		private static final long serialVersionUID = -8919993719520437733L;

		public String value;
		public String label;
		public String sort;

		public OptionBean(String value, String label, String sort) {
			super();
			this.value = value;
			this.label = label;
			this.sort = sort;
		}

		public OptionBean() {
			super();
		}

		public String getValue() {
			return value;
		}

		public void setValue(String value) {
			this.value = value;
		}

		public String getPickerViewText() {
			// 这里还可以判断文字超长截断再提供显示
			return label;
		}

		public String getLabel() {
			return label;
		}

		public void setLabel(String label) {
			this.label = label;
		}

		public String getSort() {
			return sort;
		}

		public void setSort(String sort) {
			this.sort = sort;
		}

		@Override
		public String toString() {
			return "OptionBean [value=" + value + ", label=" + label + ", sort="
					+ sort + "]";
		}

		@Override
		public int describeContents() {
			return 0;
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(this.value);
			dest.writeString(this.label);
			dest.writeString(this.sort);
		}

		protected OptionBean(Parcel in) {
			this.value = in.readString();
			this.label = in.readString();
			this.sort = in.readString();
		}

		public static final Creator<OptionBean> CREATOR = new Creator<OptionBean>() {
			@Override
			public OptionBean createFromParcel(Parcel source) {
				return new OptionBean(source);
			}

			@Override
			public OptionBean[] newArray(int size) {
				return new OptionBean[size];
			}
		};
	}

	public static class OptionsBean implements Serializable {

		private static final long serialVersionUID = 6782708260324339471L;

		public List<OptionBean> business_type;
		public List<OptionBean> customer_type;
		public List<OptionBean> borrower_type;
		public List<OptionBean> official_type;
		public List<OptionBean> marriage_type;
		public List<OptionBean> education_type;
		public List<OptionBean> living_type;
		public List<OptionBean> relationship_type;

		@Override
		public String toString() {
			return "OptionsBean{" +
					"business_type=" + business_type +
					", customer_type=" + customer_type +
					", borrower_type=" + borrower_type +
					", official_type=" + official_type +
					", marriage_type=" + marriage_type +
					", education_type=" + education_type +
					", living_type=" + living_type +
					", relationship_type=" + relationship_type +
					'}';
		}
	}
}
